//
//  CTVCache.h
//  Channltv
//
//  Created by Bruno  Keymolen on 23/07/07.
//  Copyright 2007 channl.tv. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "CTVHTTPMediaDownload.h"
#import "CTVHTTPResourceDownload.h"
#import "CTVHTTPDataDownload.h"
#import "CTVClip.h"
#import "CTVWatchpointSync.h"
#import "CTVChannel.h"

/* Organistation of the cache:
 * Root:       ~/channl.tv/				> xml files from channl.tv (channels & clipsinchannel)
 * Media:      ~/channl.tv/Movies		> movies (clips) downloaded from the internet (have an extension)
 * Resources:  ~/channl.tv/Resources	> thumbnails etc
 * 
 * Cahche entrys are represented as an MD5 HASH of the URL pointing to the original content
 */

@class CTVCache, CTVCacheListener;


@protocol CTVCacheListener
	-(void)cacheFinishedForURL:(NSString*)url andStatusCode:(int)statusCode;
@end


@interface CTVCache : NSObject <CTVMediaDownloadReply, CTVResourceDownloadReply, CTVDataDownloadReply> {
	NSMutableDictionary* m_mediaDownloads;
	NSMutableDictionary* m_resourceDownloads;
	NSMutableDictionary* m_dataDownloads;
	NSMutableArray* m_listeners;
}

+(id)GetInstance;
+(NSString*)getCachePathForURL:(NSString*)urlString;

+(NSString*)getMediaCachePathForURL:(NSString*)urlString;
+(NSString*)getResourceCachePathForURL:(NSString*)urlString;
+(NSString*)getDataCachePathForURL:(NSString*)urlString;

+(NSString*)getMediaCacheEntryForURL:(NSString*)urlString; //manages the extensions
+(NSString*)getHashString:(NSString*)urlString;
+(BRBitmapTexture*)createBitmapTextureFromPath:(NSString*)path withScene:(BRRenderScene*)scene;
+(BOOL)urlExist:(NSString*)urlString;

+(BRBitmapTexture*)getStreamTextureForScene:(BRRenderScene*)scene;
+(BRBitmapTexture*)getOnDiskTextureForScene:(BRRenderScene*)scene;
+(BRBitmapTexture*)getWatchPointTextureForScene:(BRRenderScene*)scene;
+(BRBitmapTexture*)getRateTextureForScene:(BRRenderScene*)scene andRate:(float)rate;
+(BRBitmapTexture*)getBigRateTextureForScene:(BRRenderScene*)scene andRate:(float)rate;
+(BRBitmapTexture*)_getRateTextureForScene:(BRRenderScene*)scene andRate:(float)rate andPrefix:(NSString*)prefix;
+(BRBitmapTexture*)_getBigRateTextureForScene:(BRRenderScene*)scene andRate:(float)rate andPrefix:(NSString*)prefix;




-(CTVHTTPMediaDownload*)isDownloadingURLString:(NSString*)url;
-(BOOL)isMediaInCacheForURLString:(NSString*)url;

-(CGImageRef)CreateImageForURLString:(NSString*)urlString;
-(void)clearCache;//DEPRECATED

-(NSString*)cacheData:(NSString*)url; //Return data path
-(NSString*)cacheResource:(NSString*)url; //Return data path
-(NSString*)cacheMedia:(NSString*)url andClip:(CTVClip*)clip; //Return data path (releated to cacheURLString)


-(CTVHTTPResourceDownload*)cacheResourceURLString:(NSString*)urlstring cachePath:(NSString**)cachePath;
-(CTVHTTPMediaDownload*)cacheURLString:(NSString*)url cachePathNoExt:(NSString**)cachePath andClip:(CTVClip*)clip;
-(CTVHTTPDataDownload*)cacheDataURLString:(NSString*)urlstring cachePath:(NSString**)cachePath;

//Cache listeners (add/remove)
-(void)addCacheListener:(CTVCacheListener*)listener;
-(void)removeCacheListener:(CTVCacheListener*)listener;

//Listeners (delegate methods)
-(int)finishResourceDownload:(CTVHTTPResourceDownload*)resourceDownload;
-(int)releaseResourceDownload:(CTVHTTPResourceDownload*)resourceDownload;

-(int)finishDataDownload:(CTVHTTPDataDownload*)dataDownload;
-(int)releaseDataDownload:(CTVHTTPDataDownload*)dataDownload;

-(int)finishMediaDownload:(CTVMediaDownload*)mediaDownload;
-(int)releaseMediaDownload:(CTVMediaDownload*)mediaDownload;

//Watchpoint
-(int)saveWatchPoint:(CTVChannel*)channel;
-(int)loadWatchPoint:(CTVChannel*)channel;


//Functions intended to be private
-(void)_informListenersUrlCacheFinished:(NSString*)urlString andStatusCode:(int)status;

@end
